#-*- coding:UTF-8 -*-

"""
show the width and height distribution of the images in the specified folder.
"""

import os, cv2
import matplotlib.pyplot as plt

def imageSizeDistribution(folder, show = False):
    names = os.listdir(folder)
    if len(names) == 0: return
    width = []
    height = []
    for name in names:
        path = folder + '\\' + name
        if path.find('png') == -1: continue  # in case of other files
        img = cv2.imread(path)
        height.append(img.shape[0])
        width.append(img.shape[1])
    if show == True:
        plt.plot(height, width, 'o')
        plt.show()
    return width, height

def eachSizeDistribution(data, show = False):
    if len(data) == 0: return {}
    data.sort()
    distri = {}
    count = 1
    size = data[0]
    for s in data[1:]:
        if s == size: count += 1
        else:
            distri[size] = count
            size = s
            count = 1
    # in case of the last element is just a cluster
    if count == 1: distri[size] = count
    size = distri.keys()
    count = distri.values()
    if show == True:
        plt.bar(size, count)
        plt.show()
    return distri
    
    
